<?php
    class taobao{

        var $license = '';

        function taobao($license){
            $GLOBALS['system'] = $this;//该方法时初始化DAPI中的$GLOBALS['system']，为了调用下面的loadModel方法,loadModel具体可重写
            $this->license = $license;
        }

        function loadModel($modelname){
            $Md_name = explode("/",$modelname);
            require_once("mdl.".$Md_name[1].".php");
            $md_name = "mdl_".$Md_name[1];
            $model = new $md_name;
            return $model;
        }
        function testget_app_key(){
            if(!class_exists('mdl_apiclient')){
                require_once("mdl.apiclient.php");
            }
            $center = new mdl_apiclient();
            $center->key = '371e6dceb2c34cdfb489b8537477ee1c';
            $center->url = 'http://esb.shopex.cn/api.php';
            return $center->native_svc("service.get_appkey",array('certificate_id'=>$this->license));//certificate_id为网店的证书ID
        }
        function test_open_services(){//开通服务
            if(!class_exists('mdl_apiclient')){
                require_once("mdl.apiclient.php");
            }
            $center = new mdl_apiclient();
            $center->key = '371e6dceb2c34cdfb489b8537477ee1c';
            $center->url = 'http://esb.shopex.cn/api.php';
            return $center->native_svc("service.open_services",array('certificate_id'=>$this->license,'services'=>'taobao_goods_synchronize,taobao_order_synchronize,taobao_goods_sales'));//certificate_id为网店的证书ID
        }

        function save_nick($taobao_acc){//绑定
            if(!class_exists('mdl_apiclient')){
                require_once("mdl.apiclient.php");
            }
            $center = new mdl_apiclient();
            $center->key = '371e6dceb2c34cdfb489b8537477ee1c';
            $center->url = 'http://esb.shopex.cn/api.php';
            $ee=$center->native_svc("session.save_tb_nick",array('certificate_id'=>$this->license,'tb_nickname'=>$taobao_acc));
            //tb_nickname=>绑定的用户名
        }

        function get_nick(){//获取绑定的用户名
            if(!class_exists('mdl_apiclient')){
                require_once("mdl.apiclient.php");
            }
            $center = new mdl_apiclient();
            $center->key = '371e6dceb2c34cdfb489b8537477ee1c';
            $center->url = 'http://esb.shopex.cn/api.php';
            return $center->native_svc("session.get_tb_nick",array('certificate_id'=>$this->license));
        }


        function service_valid(){//服务验证
            if(!class_exists('mdl_apiclient')){
                require_once("mdl.apiclient.php");
            }
            $center = new mdl_apiclient();
            $center->key = '371e6dceb2c34cdfb489b8537477ee1c';
            $center->url = 'http://esb.shopex.cn/api.php';
            $params = array('certificate_id'=>$this->license,
                            'version'=>'1.0',//不变
                            'service_id'=>'taobao_goods_sales');
            //service_id一共三种
            //taobao_goods_sales 销售记录下载
            //taobao_order_synchronize 订单同步
            //taobao_goods_synchronize 商品上传
            $center->native_svc("session.get_tb_nick",$params);
        }

        function printapp_key(){
            print_r($this->testget_app_key());
        }

        function checktb_sign(){
            $mess = $this->testget_app_key();
            if($mess['result']=='succ'){
                $app_secret = $mess['result_msg']['app_secret'];
            }else{
                echo 'APPSERECT获取失败';
                exit;
            }
            $params = $_GET;
            $sign = base64_encode($this->md5bin(md5($params['top_appkey'].$params['top_parameters'].$params['top_session'].$app_secret)));
            if($params['top_sign']==$sign){
                echo '验证成功';
            }
        }



        function md5bin($md5str){
            $ret = '';
            for($i=0;$i<32;$i+=2){
                $ret.=chr(hexdec($md5str{$i}.$md5str{$i+1}));
            }
            return $ret;
        }

        function makeSign($tbs_params){
            ksort($tbs_params);
            $sign = $tbs_params['app_secret'];
            foreach($tbs_params as $key=>$value)
            {
                if($key!='app_secret')
                {
                    $sign .= $key.$value;
                }
            }
            $sign .= $tbs_params['app_secret'];
            $sign = strtoupper(md5($sign));
            return $sign;
        }

        function testgetUserInfo($info_value,$tb_session,$ss='get'){
            if(!function_exists("dapi_call")){
                require("dapi.php");
            }
            $mess = $this->testget_app_key();
            $params=array_merge($mess['result_msg'],$info_value);
            //此处$mess['result_msg']为中心取得API链接APPKEY以及私钥为必要
            //第二个参数则是淘宝输入的参数,参数详细见:http://open.taobao.com/dev/index.php/API%E6%96%87%E6%A1%A3
            $is_echop=true;

            $session = $tb_session;

            $mess = dapi_call('tb_mess_send',array($params,$session,$ss,$is_echop));

            return $mess;
        }

        function testloginurl($user){
        $mess = $this->testget_app_key();
        if($mess['result']=='succ'){
            $appkey = $mess['result_msg']['app_key'];
            $app_secret = $mess['result_msg']['app_secret'];
        }else{
            echo 'APPkey获取失败';
            exit;
        }

        $cur_path = empty($_SERVER['REQUEST_URI']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['REQUEST_URI'];
        $url=dirname($GLOBALS['ecs']->get_domain() . $cur_path);

        $tbs_params['app_key'] = $appkey;
        $tbs_params['app_secret'] = $app_secret;
        $tbs_params['sign_method'] = 'md5';
        $tbs_params['timestamp'] = date('Y-m-d H:i:s',time());
        //$tbs_params['ext_shop_title'] = "shopex网店";//此处为登陆时的合作现实
        //$tbs_params['ext_shop_domain'] = "http://www.shopex.cn";//网站的域名;
        //$tbs_params['action'] = 'logon';
        $tbs_params['target'] = substr($url.'/taobao_value.php',7);//登陆后返回的URL

        $login_url = "http://container.api.taobao.com/container/identify";
        foreach($tbs_params as $key=>$value){
            if($key!='app_secret')
            {
                $ps_s[]= $key.'='.$value;
            }
        }
        $tb_url = $login_url.'?'.implode("&",$ps_s);
        header("Location:".$tb_url."&sign=".$this->makeSign($tbs_params));
        }

    }

if(!function_exists('json_decode')){
    function json_decode($json,$assoc){
        include_once(ROOT_PATH . 'includes/shopex_json.php');
        $o = new Services_JSON();
        return $o->decode($json,$assoc);
    }
}

if (!function_exists('http_build_query'))
{
    function http_build_query($arr)
    {
        $restr = '';
        foreach ($arr as $key => $val)
        {
            $restr .= $key . '=' . $val . '&';
        }
        $restr = str_replace(' ', '+', $restr);
        $restr = substr($restr, 0, -1);

        return $restr;
    }
}

?>